package net.thetabx.ftdba;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.thetabx.ftdba.http.HttpData;
import net.thetabx.ftdba.http.HttpManager;


import android.content.res.Resources;

public class Userstats {
	private HttpManager http;
	private RStatus status = RStatus.OK;
	private String classId;
	private String nickname;
	private int nMp;
	
	private String ratio;
	private String upload;
	private String download;
	private String buffer;
	
	private String gRatio;
	private String gUpload;
	private String gDownload;
	private String gBuffer;
	
	private String seedtime;
	private String sltr;
	private Resources res;
	
	public Userstats(HttpManager http) {
		this.http = http;
		res = GlobalData.getContext().getResources();
		pullUserstats();
	}
	
	public String getClassId() {
		return classId;
	}
	
	public String getNickname() {
		return nickname;
	}

	public int getnMp() {
		return nMp;
	}

	public String getRatio() {
		return ratio;
	}

	public String getUpload() {
		return upload;
	}

	public String getDownload() {
		return download;
	}

	public String getBuffer() {
		return buffer;
	}

	public String getGRatio() {
		return gRatio;
	}

	public String getGUpload() {
		return gUpload;
	}

	public String getGDownload() {
		return gDownload;
	}

	public String getGBuffer() {
		return gBuffer;
	}

	public String getSeedtime() {
		return seedtime;
	}

	public String getSltr() {
		return sltr;
	}
	
	private void pullUserstats() {
		if(http == null) {
			status = RStatus.NO_HTTP;
			return;
		}
		
		HttpData data = http.send("/?section=ACCOUNT");
		if(data.getStatus() != RStatus.ENDED) {
			status = data.getStatus();
			return;
		}
		
		StringBuilder html = data.getContentSb();
		
		//Matcher mNickname = Pattern.compile("Bienvenue <a href=\"/\\?section=ACCOUNT\" class=\"class_(\\d+)\" title=\"Consulter votre compte\">(\\w+)</a>").matcher(html);
		Matcher mNickname = Pattern.compile("Aper�u de mon compte \\[ <b class=\"class_(\\d+)\">(\\w+)</b> \\]").matcher(html);
		if(mNickname.find()) {
			classId = mNickname.group(1);
			nickname = mNickname.group(2);
		}
		else {
			classId = "0";
			nickname = res.getString(R.string.default_nickname);			
		}
			
		
		Matcher mNMp = Pattern.compile("</a>\\s+(\\d+)\\s+<a href=\"/\\?section=LOGOUT\" class=\"logout\"> <img src=\"themes/images/logout-on.png\" title=\"D�connexion\"></a>").matcher(html);
		if(mNMp.find())
			nMp = Integer.parseInt(mNMp.group(1));
		else
			nMp = 0;
		
		//Matcher mRatio = Pattern.compile("<div class=\"ratio\" title=\"Ratio Global du volume partag� par rapport au volume t�l�charg�\">\\s+([\\d\\.,]+)\\s+</div>").matcher(html);
		Matcher mRatio = Pattern.compile("<img src=\"themes/images/medal_gold\\.png\" /> ([\\d\\.,]+|Inf)</div>").matcher(html);
		if(mRatio.find())
			ratio = mRatio.group(1);
		else
			ratio = "Inf ?";
		
		Matcher mGRatio = Pattern.compile("<img src=\"themes/images/ratio\\.png\" /> ([\\d\\.,]+|Inf)</div>").matcher(html);
		if(mGRatio.find())
			gRatio = mGRatio.group(1);
		else
			gRatio = "Inf ?";
		
		//Matcher mUpload = Pattern.compile("<div class=\"up\" title=\"Volume de donn�es envoy�es\">\\s+([\\d\\.]+ \\wB)\\s+</div>").matcher(html);
		Matcher mUpload = Pattern.compile("Upload</span><img src=\"themes/images/arrow_up_green.png\" /> ([\\d\\.]+ \\wB)</div>").matcher(html);
		if(mUpload.find())
			upload = mUpload.group(1);
		else
			upload = res.getString(R.string.default_upload);
		
		Matcher mGUpload = Pattern.compile("Upload Global</span><img src=\"themes/images/arrow_up_green.png\" /> ([\\d\\.]+ \\wB)</div>").matcher(html);
		if(mGUpload.find())
			gUpload = mGUpload.group(1);
		else
			gUpload = res.getString(R.string.default_upload);
		
		//Matcher mDownload = Pattern.compile("<div class=\"down\" title=\"Volume de donn�es t�l�charg�es\">\\s+([\\d\\.]+ \\wB)\\s+</div>").matcher(html);
		Matcher mDownload = Pattern.compile("Download</span><img src=\"themes/images/arrow_down_blue.png\" /> ([\\d\\.]+ \\wB)</div>").matcher(html);
		if(mDownload.find())
			download = mDownload.group(1);
		else
			download = res.getString(R.string.default_download);
		
		Matcher mGDownload = Pattern.compile("Download Global</span><img src=\"themes/images/arrow_down_blue.png\" /> ([\\d\\.]+ \\wB)</div>").matcher(html);
		if(mGDownload.find())
			gDownload = mGDownload.group(1);
		else
			gDownload = res.getString(R.string.default_download);
		
		//Matcher mBuffer = Pattern.compile("<div class=\"buff\">\\s+([\\+\\-] [\\d\\.]+ \\wB)\\s+</div>").matcher(html);
		Matcher mBuffer = Pattern.compile("80px; color: #171e6b; font-weight: bold; font-size: 11px\">Buffer</span><img src=\"themes/images/arrow_buffer.png\" /> ([\\+-] [\\d\\.]+ \\wB)</div>").matcher(html);
		if(mBuffer.find())
			buffer = mBuffer.group(1);
		else
			buffer = res.getString(R.string.default_buffer);
		
		Matcher mGBuffer = Pattern.compile("110px; color: #171e6b; font-weight: bold; font-size: 11px\">Buffer</span><img src=\"themes/images/arrow_buffer.png\" /> ([\\+-] [\\d\\.]+ \\wB)</div>").matcher(html);
		if(mGBuffer.find())
			gBuffer = mGBuffer.group(1);
		else
			gBuffer = res.getString(R.string.default_buffer);
		
		//Matcher mSeedtime = Pattern.compile("SeedTime: <i>((\\d+j )?(\\d+h )?(\\d+m)) </i>").matcher(html);
		Matcher mSeedtime = Pattern.compile("<label>Seedtime Actuel</label>\\s*<span>((\\d+j )?(\\d+h )?(\\d+m )?(\\d+s)) </span>").matcher(html);
		if(mSeedtime.find())
			seedtime = mSeedtime.group(1);
		else
			seedtime = "-------------";
		
		//Matcher mSltr = Pattern.compile("SLTR: <i>([\\d\\.]+)</i>").matcher(html);
		Matcher mSltr = Pattern.compile("<label>SLTR</label>\\s*<span>([\\d\\.]+)</span>").matcher(html);
		if(mSltr.find())
			sltr = mSltr.group(1);
		else
			sltr = res.getString(R.string.default_sltr);
		
		status = RStatus.ENDED;
	}

	public RStatus getStatus() {
		return status;
	}

}
